package genutil

import (
	"math/rand"
	"strconv"
	"strings"
	"time"
)

const constStrSource = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

// RandStr 生成指定长度的随机字符串
func RandStr(length int) string {
	rand.Seed(time.Now().UnixNano())
	bound := len(constStrSource)
	buf := make([]byte, 0, length)
	for i := 0; i < length; i++ {
		buf = append(buf, constStrSource[rand.Intn(bound)])
	}
	return string(buf)
}

// GenVerifyCode6Num 生成6位数字验证码
func GenVerifyCode6Num() string {
	rand.Seed(time.Now().UnixNano())
	buf := strings.Builder{}
	buf.Grow(6)
	for i := 0; i < 6; i++ {
		buf.WriteByte('0' + byte(rand.Intn(10)))
	}
	return buf.String()
}

// GenOrderNo 生成订单号 timestamp: 秒
func GenOrderNo(timestamp int64, userId int64) string {
	return strconv.FormatInt(timestamp, 10) + "-" + strconv.FormatInt(userId, 10)
}
